/**
 * Author : jinhaibin
 * Date   : 2018/5/10
 * Time   : 下午2:42
 * desc   :
 */

import Vue from 'vue';

export default {
  namespaced: true,
  state: {
    breadcrumb: [], // 面包屑导航
    routers: [], // 打开状态的路由列表
    active: '' // 当前展示的路由
  },
  mutations: {
    /**
     * 设置面包屑导航
     */
    setBreadcrumb(state, data) {
      Vue.set(state, 'breadcrumb', data);
    },
    /**
     * 添加打开状态的路由
     * @param state
     * @param router
     */
    addRouter(state, route) {
      let result = state.routers.find(item => route.id === item.id);
      // 不存在在添加
      if (!result) {
        state.routers.push(route);
      }
      // 设置为激活状态
      state.active = route.id;
    },
    /**
     * 关闭指定标签
     * @param state
     * @param tag
     */
    closeTag(state, tag) {
      let tagIndex = state.routers.findIndex(item => item.id === tag.id);
      // 只有一个元素的情况下不允许删除
      if (state.routers.length > 1) {
        state.routers.splice(tagIndex, 1);
      }
    },
    /**
     * 关闭其他标签
     * @param state
     * @param tag
     */
    closeOthersTag(state, tag) {
      Vue.set(state, 'routers', [tag]);
      state.active = tag.id;
    },
    /**
     * 关闭左侧标签
     * @param state
     * @param tag
     */
    closeLeftTag(state, tag) {
      let tagIndex = state.routers.findIndex(item => item.id === tag.id);
      state.routers.splice(0, tagIndex);
    },
    /**
     * 关闭右侧标签
     * @param state
     * @param tag
     */
    closeRightTag(state, tag) {
      let tagIndex = state.routers.findIndex(item => item.id === tag.id);
      state.routers.splice(tagIndex + 1, state.routers.length - tagIndex);
    }
  },
  getters: {
    getRouters(state) {
      return state.routers;
    },
    getActive(state) {
      return state.active;
    },
    /**
     * 获取面包屑导航
     * @param state
     */
    getBreadcrumb(state) {
      return state.breadcrumb;
    }
  }
};
